import bridge from '../../node_modules/dsbridge/index'
import {
  isAndroid,
  isIOS
} from '../common/util.js'

/**
 * 从移动端获取AccessToken
 */
export const getAccessTokenOld = () => {
  return bridge.call('getAccessToken', '')
}

export const getAccessToken = (callback) => {
  setTimeout(function () {
    var token = process.env.NODE_ENV === 'production' ? getAccessTokenOld() : process.env.ACCESS_TOKEN
    callback(token)
  }, 1000)
}

/**
 * 调起原生的半屏登录框
 */
export const showLoginView = () => {
  return bridge.call('showLoginView', '')
}

/**
 * 设置原生导航栏的标题
 * @param {*} title 标题
 * @param {*} callback 回调
 */
export const setNativeTitle = (title, callback) => {
  return bridge.call('setNativeTitle', title, callback)
}

/**
 * 万能跳转到原生页面
 * @param {*} iOSParams iOS端接收的参数
 * @param {*} AndroidParams 安卓端接收的参数
 * @param {*} callback 回调
 */
export const nativePush = (iOSParams, AndroidParams, callback) => {
  let params = {}
  if (isAndroid()) {
    params = AndroidParams
  } else if (isIOS()) {
    params = iOSParams
  }
  console.log(params)
  return bridge.call('naviPush', params, callback)
}

/**
 * 刷新动态详情页面的评论列表
 * @param {*} callback 回调函数
 */
export const refreshCommentList = (callback) => {
  return bridge.register('refreshCommentList', function (res) {
    if (typeof (callback) === 'function') {
      callback()
    }
  })
}

/**
 * 设置原生编辑框的评论数量
 * @param {*} count 评论数量
 */
export const setCommentCount = (count) => {
  return bridge.call('setCommentCount', count)
}

/**
 * 滑动详情页至评论列表
 * @param {*} callback 函数参数
 */
export const scrollToCommentList = (callback) => {
  return bridge.register('scrollToCommentList', function (res) {
    if (typeof (callback) === 'function') {
      callback()
    }
  })
}
